package cn.bdqn.controller;


import cn.bdqn.bean.result.Result;
import cn.bdqn.bean.result.ResultCode;
import cn.bdqn.bean.result.ResultGenerator;
import cn.bdqn.domain.*;
import cn.bdqn.service.*;
import com.google.inject.internal.asm.$Type;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * @author 收费信息的controller
 */
@RestController
@RequestMapping("/OPC")
public class ChargeController {

    @Autowired
    private ChargeService chargeService;
    @Autowired
    private MenberService menberService;
    @Autowired
    private RegistrationService registrationService;
    @Autowired
    private RecipeService recipeService;
    @Autowired
    private HospitalizedService hospitalizedService;

    /**
     * 根据病人信息id查询收费信息
     *
     * @param menberId
     * @param map
     * @return
     */
    @RequestMapping("/queryByMenberId/{menberId}")
    public Result queryByMenderId(@PathVariable("menberId") Integer menberId, Map map) {
        Charge charge = null;
        try {
            charge = chargeService.queryByMenberId(menberId);
            map.put("charge", charge);
            return ResultGenerator.genSuccessResult(map);
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genFailResult("查询失败", ResultCode.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 添加收费单
     *
     * @param charge
     * @return
     */
    @RequestMapping("/addCharge")
    public Result addCharge(Charge charge) {
        int selectCount;
        int insertCount;
        try {
                insertCount = chargeService.addCharge(charge);
                if (insertCount == 1) {
                    if (charge.getType() == 1) {
                        getMedicine(charge.getMenberId(), charge);
                        return ResultGenerator.genSuccessResult("医药费用支付成功");
                    } else if (charge.getType() == 0) {
                        getHospitalCosts(charge.getMenberId(), charge);
                        return ResultGenerator.genSuccessResult("住院费用支付成功");
                    } else {
                        return ResultGenerator.genSuccessResult("失败");
                    }
                } else {
                    return ResultGenerator.genSuccessResult("添加失败");
                }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genFailResult("失败", ResultCode.INTERNAL_SERVER_ERROR);
        }
    }

    /**
     * 支付医药费用的方法
     *
     * @param menberId
     * @param charge
     * @return
     */
    public Result getMedicine(int menberId, Charge charge) {
        Registration registrations;
        int updateRegisCount;
        int registrationCount;
        try {
            //根据menberId查询挂号信息
            registrations = registrationService.queryByMenberId(menberId);
            //根据挂号id修改处方表支付的状态
            updateRegisCount = recipeService.updateByRegisId(registrations.getId());
            if (updateRegisCount == 1) {
                //修改挂号信息的支付状态
                registrationCount = registrationService.updateByMenberId(menberId);
                if (registrationCount == 1) {
                    Menber menber = menberService.queryById(menberId);
                    Double totalMoney = charge.getTotalMoney().doubleValue();
                    Double menberPrestore = menber.getMenberPrestore();
                    Double result = menberPrestore - totalMoney;
                    menber.setMenberPrestore(result);
                    menberService.updateMenberPrestoreByMenberId(charge.getMenberId(), result);
                    return ResultGenerator.genSuccessResult("支付成功");
                } else {
                    return ResultGenerator.genSuccessResult("修改挂号信息的支付状态失败");
                }
            } else {
                return ResultGenerator.genSuccessResult("修改处方表支付的状态失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genFailResult("支付失败");
        }
    }

    /**
     * 支付住院费用
     *
     * @param menberId
     * @return
     */
    public Result getHospitalCosts(int menberId, Charge charge) {
        Registration registrations;
        int updateByRecipeIdCount;
        try {
            //根据menberId查询挂号信息
            registrations = registrationService.queryByMenberId(menberId);
            //根据处方id修改住院表的支付状态
            updateByRecipeIdCount = hospitalizedService.updateByRegistrationId(registrations.getId());
            if (updateByRecipeIdCount == 1) {
                    Menber menber = menberService.queryById(menberId);
                    Double totalMoney = charge.getTotalMoney().doubleValue();
                    Double menberPrestore = menber.getMenberPrestore();
                    Double result = menberPrestore - totalMoney;
                    menber.setMenberPrestore(result);
                    menberService.updateMenberPrestoreByMenberId(charge.getMenberId(), result);
                    return ResultGenerator.genSuccessResult("支付成功");
            } else {
                return ResultGenerator.genSuccessResult("修改住院表的支付状态失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return ResultGenerator.genSuccessResult("失败");
        }
    }

    /**
     * 查询全部收费信息
     *
     * @return
     */
    @RequestMapping("/queryAll")
    public Result queryAll() {
        List<Charge> charge = chargeService.queryAll();
        return ResultGenerator.genSuccessResult(charge);
    }
}
